package senpec.qc.harmony.base.mybatisplus;

import com.baomidou.mybatisplus.annotation.OrderBy;
import com.baomidou.mybatisplus.annotation.Version;
import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import senpec.qc.harmony.system.user.User;
import tools.jackson.databind.annotation.JsonDeserialize;
import tools.jackson.databind.annotation.JsonSerialize;
import tools.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import tools.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;

import java.io.ObjectInputFilter;
import java.io.Serializable;
import java.time.LocalDateTime;

@EqualsAndHashCode(callSuper = true)
@Data
@MappedSuperclass
public class IBaseEntity<T extends Model<?>> extends Model<T> implements Serializable, ObjectInputFilter {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @TableId(value = "id", type = IdType.AUTO)
    @OrderBy
    private Long id;

    private Long creatorId;

    @TableField(fill = FieldFill.INSERT)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @Transient
    @TableField(exist = false)
    private User creator;

    private Long updaterId;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    @Transient
    @TableField(exist = false)
    private User updater;

    @TableLogic
    @Column(name = "deleted", nullable = false, columnDefinition = "smallint default 0")
    private Boolean deleted = false;

    @Version
    private Integer version = 0;

    @Transient
    @TableField(exist = false)
    private Integer currentPage = 0;

    @Transient
    @TableField(exist = false)
    private Integer pageSize = 20;

    @Override
    public Status checkInput(FilterInfo filterInfo) {
        return Status.ALLOWED;
    }
}

